import { ValidateIf, ValidationOptions } from 'class-validator'
import { Limits } from '@boticord/consts'

export function ValidateWebhookHeaders(validationOptions?: ValidationOptions) {
  return ValidateIf((obj, value) => {
    return typeof value === 'object'
      && value !== null
      && !Array.isArray(value)
      && Object.entries(value).every(([ k, v ], inx) => {
        return typeof k === 'string' && typeof v === 'string'
          && k.length > 0 && k.length <= Limits.MaxWebhookHeaderKeyLength && !k.startsWith(' ')
          && v.length > 0 && v.length <= Limits.MaxWebhookHeaderValueLength && !v.startsWith(' ')
          && (inx + 1) < Limits.MaxWebhookHeaders
      })
  }, validationOptions)
}